package com.open.party.cloud.view

import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import com.open.party.cloud.R
import com.open.party.cloud.model.ArticleEntity
import com.open.party.cloud.model.ArticleVo
import com.open.party.cloud.model.DictionaryBean
import com.open.party.cloud.model.SlideshowListBean
import com.open.party.cloud.view.comm.view.banner.CustomViewHolder
import com.open.party.cloud.view.home.article.ArticleDetailsActivity
import com.open.party.cloud.view.home.article.fragment.ArticleListFragment
import com.open.party.cloud.view.home.jiCengDangJian.OrgFor1QxListActivity
import com.open.party.cloud.view.home.ziShenJiaoYu.ZiShenJianSheLeaderListInfoActivity
import com.open.party.cloud.viewModel.ArticleViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperListener
import com.sinothk.banner.nice.BannerConfig
import com.sinothk.banner.nice.Transformer
import kotlinx.android.synthetic.main.home_fragment_main.*
import kotlinx.android.synthetic.main.home_fragment_main1112.*
import kotlinx.android.synthetic.main.home_fragment_main1112.bannerView
import kotlinx.android.synthetic.main.home_fragment_main1112.baseOrgItem
import kotlinx.android.synthetic.main.home_fragment_main1112.leaderItem
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Deprecated("弃用")
class HomeMainFragment1112 : HomeMainBaseFragment1112(), OnSuperListener<ArticleEntity> {

    private var viewModel: ArticleViewModel? = null
    private var currView: View? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (currView == null) {
            currView = inflater.inflate(R.layout.home_fragment_main1112, container, false)
        }
        return currView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel = ArticleViewModel()
        initView()
    }

    private fun initView() {
        initListData()
        initTabView()

        leaderItem.setOnClickListener {
            XUtils.intent().openActivity(activity, ZiShenJianSheLeaderListInfoActivity::class.java)
                .startInFragment(this)
        }
        baseOrgItem.setOnClickListener {
            XUtils.intent().openActivity(activity, OrgFor1QxListActivity::class.java).startInFragment(this)
        }

    }

    // ==================================================================
    override fun onResume() {
        super.onResume()
        EventBus.getDefault().register(this)
    }

    override fun onPause() {
        super.onPause()
        EventBus.getDefault().unregister(this)
    }

    /**
     * 轮播图
     */
    private fun initBannerView(records: MutableList<ArticleEntity>) {
        val titles: ArrayList<String> = java.util.ArrayList()
        val bannerList: ArrayList<SlideshowListBean> = ArrayList()
        for (item in records) {
            titles.add(item.theme)
            bannerList.add(SlideshowListBean(item.id, item.imgUrl))
        }

        bannerView!!.setBannerTitles(titles)
            .setAutoPlay(true)
            .setPages(bannerList, CustomViewHolder())
            .setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE)
            .setBannerAnimation(Transformer.Default)
            .setDelayTime(3000)
            .start()

        bannerView!!.setOnBannerClickListener { _, position ->
            val id = bannerList[position].id
            if (TextUtils.isEmpty(id)) {
                return@setOnBannerClickListener
            }
            XUtils.intent().openActivity(activity, ArticleDetailsActivity::class.java)
                .putStringExtra("id", bannerList[position].id)
                .startInFragment(this)
        }
    }

    private fun initListData() {
        // 轮播图
        initViewListData("HOME_LBXW")
    }

    private fun initViewListData(type: String) {
        val pageReq = PageReq<ArticleVo>()
        pageReq.pageNo = 1
        pageReq.pageSize = 6
        val vo = ArticleVo()
        vo.childrenType = DictionaryBean(type, "")
        pageReq.data = vo

        viewModel!!.getArticleListData(pageReq, type)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun receiveStudentEventBus(result: ResultInfo<AppPageData<ArticleEntity>>) {

        if ("HOME_LBXW" == result.eventType || "HOME_SZYW" == result.eventType || "HOME_TZGG" == result.eventType) {

            when (result.code) {
                ResultInfo.SUCCESS -> {
                    if (result.data != null && result.data.records != null) {

                        when (result.eventType) {
                            "HOME_LBXW" -> {
                                initBannerView(result.data.records)
                            }
                        }

                    } else {
                    }
                }
                ResultInfo.TOKEN_OVERDUE -> {
                    // 不存在
                }
                else -> {
                    XUtils.toast().show(result.msg)
                }
            }
        } else {
            return
        }
    }

    override fun onClick(position: Int, itemData: ArticleEntity, flag: String) {
        XUtils.intent().openActivity(activity, ArticleDetailsActivity::class.java)
            .putStringExtra("id", itemData.id)
            .startInFragment(this)
    }

    private fun initTabView() {
        val titleList = ArrayList<String>()
        titleList.add("时政要闻")
        titleList.add("组工动态")
        titleList.add("通知公告")
        titleList.add("凉都先锋")
        titleList.add("他山之石")

        val typeList = ArrayList<String>()
        typeList.add("HOME_SZYW")
        typeList.add("HOME_ZGDT")
        typeList.add("HOME_TZGG")
        typeList.add("HOME_XJDX")
        typeList.add("HOME_TSZS")

        initTab(
            titleList,
            getFragments(typeList)
        )
    }

    private fun getFragments(typeList: java.util.ArrayList<String>): List<Fragment> {
        val fragments: ArrayList<Fragment> = ArrayList()

        for (type in typeList) {
            val fragment = ArticleListFragment()
            val bundle = Bundle()
            bundle.putString("type", type)
            fragment.arguments = bundle
            fragments.add(fragment)

            Log.e("type", type)
        }

        return fragments
    }

    private fun initTab(titles: ArrayList<String>, fragments: List<Fragment>) {

        val fragmentPagerAdapter = object : FragmentPagerAdapter(childFragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(arg0: Int): Fragment {
                return fragments[arg0]
            }
        }

        viewPager.offscreenPageLimit = titles.size - 1
        viewPager.adapter = fragmentPagerAdapter
        //        pager.addOnPageChangeListener(this);

        scrollTab.setTitles(titles, 0, titles.size <= 4)
//        scrollTab.setNumber(1, "9", View.VISIBLE)//设置数字红点
        scrollTab.setViewPager(viewPager)
        scrollTab.setOnTabListener { index, _ ->
            viewPager.setCurrentItem(index, true)
        }
    }
}